package com.jiangchao.mimidao.framework.factory;

import com.jiangchao.mimidao.framework.aop.MimiDaoHandler;
import java.lang.reflect.Proxy;
import org.springframework.beans.factory.FactoryBean;

public class MimiDaoBeanFactory<T> implements FactoryBean<T> {

  private Class<T> daoInterface;

  private MimiDaoHandler proxy;

  public T getObject() throws Exception {
    return newInstance();
  }

  public Class<?> getObjectType() {
    return daoInterface;
  }

  public boolean isSingleton() {
    return true;
  }

  private T newInstance() {
    return (T) Proxy
        .newProxyInstance(daoInterface.getClassLoader(), new Class[]{daoInterface}, proxy);
  }

  public MimiDaoHandler getProxy() {
    return proxy;
  }

  public void setDaoInterface(Class<T> daoInterface) {
    this.daoInterface = daoInterface;
  }

  public void setProxy(MimiDaoHandler proxy) {
    this.proxy = proxy;
  }
}
